﻿using System;
using VIT.Web.UI;
using VIT.Web.Controls;
using VIT.Provider;
using VIT.Library.Web;
using System.Data;
using System.Collections.Generic;

namespace VIT.Web.Admin
{
    public partial class FunctionConfig : VITComponent
    {
        VIT.Provider.Function obj;

        protected void Page_Load(object sender, EventArgs e)
        {
            obj = new VIT.Provider.Function();
            if (Request["act"] == "Del")
            {
                Delete(); btnSave.Visible = false;
            }
            else
            {
                obj.Name = Request["sName"] == null ? "" : Request["sName"];                           

                if (obj.Name != "") btnAdd.Visible = true; else btnAdd.Visible = false;
            }    
            if (!IsPostBack && obj.Name!="") LoadData();
        }

        #region Methods
        [System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Select)]
        public DataTable GetData(string SortColumn)
        {
            return new VIT.Provider.Function().getAll(SortColumn);
        }

        private void LoadData()
        {
            obj.GetSingle();
            txtName.Text = obj.Name;
            txtPage.Text = obj.Page;
            txtDescription.Text = obj.Description;
            txtName.ReadOnly = true;
        }

        private bool save()
        {
            // bat loi tren server
            var data = new Dictionary<dynamic, List<object>> {
                                                        {txtName, new List<object> {"required"}},
                                                        {txtPage, new List<object> {"required"}}
                                                    };
            if (MainCore.checkData(data))
            {
                obj.Name = txtName.Text;
                obj.Page = txtPage.Text;
                obj.Description = txtDescription.Text;

                return obj.Save();
            }
            return false;
        }

        private void setTrueVisible(System.Web.UI.HtmlControls.HtmlGenericControl control)
        {
            lblThanhCong.Visible = false;
            lblThatBai.Visible = false;
            lblDelete.Visible = false;

            control.Visible = true;
        }

        private void Delete()
        {
            string id = Request["sName"] == null ? "" : Request["sName"];                        

            if (id != "")
                if (obj.Delete(id)) setTrueVisible(lblDelete);
                else setTrueVisible(lblThatBai);
        }
        #endregion

        
        #region Events
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            RedirectComponent("Config", "FunctionConfig");
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (save()) setTrueVisible(lblThanhCong);
            else setTrueVisible(lblThatBai);
            ListView.DataBind();
        }
        #endregion
    }
}
